package com.tanker.common.utils;

import java.math.RoundingMode;
import java.security.MessageDigest;
import java.text.NumberFormat;

/**
 * @author Administrator
 * 
 */
public class StringUtil {


	public static String numberFormat(Number number, int i) {
		NumberFormat numberFormat = NumberFormat.getNumberInstance();
		numberFormat.setMinimumFractionDigits(i);
		numberFormat.setMaximumFractionDigits(i);
		numberFormat.setRoundingMode(RoundingMode.HALF_UP);
		return number == null ? null : numberFormat.format(number);
	}

	public static boolean nullOrBlank(String param) {
		return (param == null || param.length() == 0 || param.trim().equals("")) ? true
				: false;
	}

	public static String notNull(String param) {
		return param == null ? "" : param.trim();
	}

	public static boolean isNumber(String str) {
		int num = 0;
		if (str == null)
			return false;
		for (int i = 0; i < str.length(); i++) {
			num = (int) str.charAt(i);
			if (num < 48 || num > 57)
				return false;
		}
		return true;
	}

	public static String encrypt(String string, String method) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = string.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance(method);
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}
}
